package com.cssw.fyzb.tps.service.sms.adapter;

import com.cssw.fyzb.tps.config.sms.ChuangLanSmsConfiguration;
import com.cssw.fyzb.tps.config.sms.SmsConfiguration;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Feng Chen
 */
public class SmsAdapterFactory {

    private static final Map<Class<? extends SmsConfiguration>, SmsAdapterCreator> CREATOR_REPOS = new HashMap<>();

    static {
        CREATOR_REPOS.put(ChuangLanSmsConfiguration.class, new ChuangLanSmsAdapterCreator());
    }

    public static SmsAdapter createSmsAdapter(SmsConfiguration configuration) {
        Class cacheConfigType;
        boolean isCompatibleType;
        Iterator it = CREATOR_REPOS.keySet().iterator();
        do {
            if (!it.hasNext()) {
                throw new IllegalArgumentException("Unrecognized configuration type: " + configuration.getClass());
            }
            cacheConfigType = (Class) it.next();
            isCompatibleType = cacheConfigType.isAssignableFrom(configuration.getClass());
        } while (!isCompatibleType);

        return CREATOR_REPOS.get(configuration.getClass()).create(configuration);
    }

    private static class ChuangLanSmsAdapterCreator implements SmsAdapterCreator<ChuangLanSmsConfiguration> {

        @Override
        public SmsAdapter create(ChuangLanSmsConfiguration configuration) {
            return new ChuangLanSmsAdapter(configuration);
        }

    }

    private static interface SmsAdapterCreator<T extends SmsConfiguration> {

        SmsAdapter create(T configuration);

    }


}
